package com.moli.iov.admin.web.controller;

import com.moli.iov.admin.enums.ListTypeEnum;
import com.moli.iov.admin.model.vo.req.AlarmHandlingVo;
import com.moli.iov.admin.model.vo.req.AlarmInfoHandlerRecordReqVo;
import com.moli.iov.admin.model.vo.req.AlarmVo;
import com.moli.iov.admin.service.IAlarmFacadeService;
import com.moli.iov.alarm.model.dto.res.AlarmInfoResDto;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.rest.RestResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 报警管理 控制器
 * </p>
 *
 * @author zql
 * @since 2019/5/27 14:54
 */
@Slf4j
@RestController
@RequestMapping("/alarm")
public class AlarmController extends BaseController {

  @Autowired
  private IAlarmFacadeService alarmFacadeService;

  /**
   * 车辆失联报警 列表
   * @return
   */
  @RequestMapping("/offline/list")
  public RestResponse<PageExtend<AlarmInfoResDto>> listPrevention(@RequestBody(required = false) AlarmVo alarmVo){
    if(alarmVo == null){
      alarmVo = new AlarmVo();
    }
    assembleUserInfo(alarmVo);
    alarmVo.setListType(ListTypeEnum.OFFLINE.getFlag());
    return RestResponse.success(alarmFacadeService.listPageAlarmInfo(alarmVo));
  }

  /**
   * 车辆失联报警 忽略
   * @return
   */
  @RequestMapping("/offline/ignore")
  public RestResponse preventionIgnore(@RequestBody(required = false)
    @Validated({AlarmVo.Ignore.class}) AlarmVo alarmVo){
    assembleUserInfo(alarmVo);
    return alarmFacadeService.ignore(alarmVo);
  }

  /**
   * 获取忽略详情  失联报警
   * @param alarmVo
   * @return
   */
  @RequestMapping("/offline/getIgnoreInfoOfIgnore")
  public RestResponse<AlarmInfoResDto> getIgnoreInfoOfIgnore(@RequestBody @Validated({AlarmVo.GetAlarmInfo.class}) AlarmVo alarmVo){
    assembleUserInfo(alarmVo);
    return alarmFacadeService.getAlarmInfo(alarmVo);
  }

  /**
   * 车辆失联报警 导出
   * @param alarmVo
   * @return
   */
  @RequestMapping("/offline/export")
  public void preventionExport(@RequestBody(required = false) AlarmVo alarmVo,
      HttpServletRequest request, final HttpServletResponse res){
    try{
      if(alarmVo == null){
        alarmVo = new AlarmVo();
      }
      assembleUserInfo(alarmVo);
      alarmVo.setListType(ListTypeEnum.OFFLINE.getFlag());
      alarmFacadeService.offLineExport(alarmVo, request, res);
    }catch(Exception e){
      log.error("车辆失联报警导出 异常", e);
    }
  }

  /**
   * 车载终端报警 列表
   * @return
   */
  @RequestMapping("/terminal/list")
  public RestResponse<PageExtend<AlarmInfoResDto>> listTerminal(@RequestBody(required = false) AlarmVo alarmVo){
    if(alarmVo == null){
      alarmVo = new AlarmVo();
    }

    assembleUserInfo(alarmVo);
    alarmVo.setListType(ListTypeEnum.TERMINAL.getFlag());
    return RestResponse.success(alarmFacadeService.listPageAlarmInfo(alarmVo));
  }

  /**
   * 车载终端报警 导出
   * @param alarmVo
   * @return
   */
  @RequestMapping("/terminal/export")
  public void terminalExport(@RequestBody(required = false) AlarmVo alarmVo,
      HttpServletRequest request, final HttpServletResponse res){
    try{
      if(alarmVo == null){
        alarmVo = new AlarmVo();
      }
      assembleUserInfo(alarmVo);
      alarmVo.setListType(ListTypeEnum.TERMINAL.getFlag());
      alarmFacadeService.terminalExport(alarmVo, request, res);
    }catch(Exception e){
      log.error("车载终端报警导出 异常", e);
    }
  }

  /**
   * 电子围栏报警 列表
   * @return
   */
  @RequestMapping("/fence/list")
  public RestResponse<PageExtend<AlarmInfoResDto>> listFence(@RequestBody(required = false) AlarmVo alarmVo){
    if(alarmVo == null){
      alarmVo = new AlarmVo();
    }

    assembleUserInfo(alarmVo);
    alarmVo.setListType(ListTypeEnum.FENCE.getFlag());
    return RestResponse.success(alarmFacadeService.listPageAlarmInfo(alarmVo));
  }

  /**
   * 报警详情  电子围栏报警
   * @param alarmVo
   * @return
   */
  @RequestMapping("/fence/getAlarmInfo")
  public RestResponse<AlarmInfoResDto> getAlarmInfo(@RequestBody @Validated({AlarmVo.GetAlarmInfo.class}) AlarmVo alarmVo){
    assembleUserInfo(alarmVo);
    return alarmFacadeService.getAlarmInfo(alarmVo);
  }

  /**
   * 电子围栏报警 忽略
   * @return
   */
  @RequestMapping("/fence/ignore")
  public RestResponse<PageExtend<AlarmInfoResDto>> fenceIgnore(@RequestBody(required = false)
    @Validated({AlarmVo.Ignore.class})AlarmVo alarmVo){
    assembleUserInfo(alarmVo);
    return alarmFacadeService.ignore(alarmVo);
  }

  /**
   * 获取忽略详情  围栏报警
   * @param alarmVo
   * @return
   */
  @RequestMapping("/fence/getIgnoreInfoOfFence")
  public RestResponse<AlarmInfoResDto> getIgnoreInfoOfFence(@RequestBody @Validated({AlarmVo.GetAlarmInfo.class}) AlarmVo alarmVo){
    assembleUserInfo(alarmVo);
    return alarmFacadeService.getAlarmInfo(alarmVo);
  }

  /**
   * 电子围栏报警 导出
   * @param alarmVo
   * @return
   */
  @RequestMapping("/fence/export")
  public void fenceExport(@RequestBody(required = false) AlarmVo alarmVo,
      HttpServletRequest request, final HttpServletResponse res){
    try{
      if(alarmVo == null){
        alarmVo = new AlarmVo();
      }
      assembleUserInfo(alarmVo);
      alarmVo.setListType(ListTypeEnum.FENCE.getFlag());
      alarmFacadeService.fenceExport(alarmVo, request, res);
    }catch(Exception e){
      log.error("电子围栏报警导出 异常", e);
    }
  }

  /**
   * 里程异变报警 列表
   * @return
   */
  @RequestMapping("/mileage/list")
  public RestResponse<PageExtend<AlarmInfoResDto>> listMileageException(@RequestBody(required = false) AlarmVo alarmVo){
    if(alarmVo == null){
      alarmVo = new AlarmVo();
    }
    assembleUserInfo(alarmVo);
    return RestResponse.success(alarmFacadeService.listMileageException(alarmVo));
  }

  /**
   * 里程异变报警 导出
   * @param alarmVo
   * @return
   */
  @RequestMapping("/mileage/export")
  public void mileageExceptionExport(@RequestBody(required = false) AlarmVo alarmVo,
      HttpServletRequest request, final HttpServletResponse res){
    try{
      if(alarmVo == null){
        alarmVo = new AlarmVo();
      }
      assembleUserInfo(alarmVo);
      alarmFacadeService.mileageExceptionExport(alarmVo, request, res);
    }catch(Exception e){
      log.error("里程异变报警导出 异常", e);
    }
  }

  /**
   * 里程异变处理 审核
   * @param alarmVo
   */
  @RequestMapping("/mileage/meAudit")
  public RestResponse meAudit(@RequestBody @Validated({AlarmVo.Audit.class}) AlarmVo alarmVo){
    assembleUserInfo(alarmVo);
    return alarmFacadeService.meAudit(alarmVo);
  }

  /**
   * 根据报警id 获取报警详情
   * @param alarmVo
   * @return
   */
  @RequestMapping("/mileage/getMileageExAlarmInfoByAlarmId")
  public RestResponse<AlarmInfoResDto> getMileageExAlarmInfoByAlarmId(@RequestBody
    @Validated({AlarmVo.GetAlarmInfo.class}) AlarmVo alarmVo){
    assembleUserInfo(alarmVo);
    return alarmFacadeService.getAlarmInfo(alarmVo);
  }
  /**
   * 编辑
   * 报警处理
   */
  @PostMapping("/offline/update")
  public RestResponse alarmHandling (@RequestBody @Validated AlarmHandlingVo vo ){
         assembleUserInfo(vo);
    return   alarmFacadeService.alarmHandling(vo);
  }

  /**
   * 报警详情
   * @return
   */
  @PostMapping("/detail")
  public RestResponse getAlarmInfoHandlerRecordList(@RequestBody @Validated  AlarmInfoHandlerRecordReqVo vo){
         assembleUserInfo(vo);
    return   alarmFacadeService.getAlarmInfoHandlerRecordList(vo);
  }
}
